/**
 * 购买产品
 */
define(['ldfw/core', 'art', 'weui','text!tpl/product/appoint.tpl','comps/session',
        'comps/ajax', 'ldfw/ldlibs/dict','ldfw/utils/format', 'ldfw/helper/formatHelper','jquery-weui'],
    function (LDFW,art,weui,tpl,session,ajax,dict) {
        var View = LDFW.Container.extend({
            className: 'panel',
            tpl: tpl,
            events:{
                'change input[name="appoint_type"]':'changeAppType',
                'click .save':'save',
                'change input[name="appo_share"]':'calculateShare',
                'click .redem':'isredem'
            },
            initialize:function(){
                this.on('shown',this.loadData)


            },

            loadData:function(){
                const _this = this
                //查询产品详情
                ajax.post('pfL.1.217',{pd_no:this.pd_no}).success(function(resp){
                    let prdocutInfo = resp.result
                    _this.model.set(prdocutInfo)
                })
                //查询是否已买过
                ajax.post('pfL.1.205',{
                    pd_no:this.pd_no
                }).success(function(resp){
                    if(resp.result.length){
                        _this.pdShare = resp.result[0].pd_share;
                    }else{
                        _this.$el.find('input[name="appoint_type"]:not([checked])').attr("disabled","disabled");
                    }

                })
            },
            rendered:function () {
                const _this = this


            },
            changeAppType:function (event) {
                const _this = this;
                var apptype=_this.$el.find('input[name="appoint_type"]:checked').val();
                if(apptype==1){
                    _this.$el.find('.apptamtshow').show();
                    _this.$el.find('.apposhareshow').hide();
                    _this.$el.find('input[name="appo_share"]').val(0).trigger('change');
                    _this.$el.find('input[name="appt_amt"]').val('').trigger('change');

                }else if(apptype==2){
                    if(!_this.pdShare){
                        $.toptip('暂未持有份额，只能进行申购预约','warning');
                        _this.$el.find('input[name="appoint_type"]').val(1).trigger('change');
                    }else{
                        _this.$el.find('.apposhareshow').show();
                        _this.$el.find('.apptamtshow').hide();
                        _this.$el.find('input[name="appt_amt"]').val(0).trigger('change');
                        _this.$el.find('input[name="appo_share"]').val('').trigger('change');
                    }

                }

            },
            isredem:function (e) {
                const _this=this;
              if(!_this.pdShare) {
                  $.toptip('当前未持有份额，只能进行申购预约','warning');
              }
            },
            calculateShare:function (event) {
                const _this = this;
                var apptype=_this.$el.find('input[name="appoint_type"]:checked').val();

                if(apptype==2){
                    var share=_this.$el.find('input[name="appo_share"]').val();
                    share=share.replace(/[^1234567890.]+/g, '');

                    if(share>_this.pdShare){
                        $.toptip('输入的份额大于持有的份额','warning');
                        _this.$el.find('input[name="appo_share"]').val(_this.pdShare?_this.pdShare:0).trigger('change');
                    }
                }
            },
            compAddedHandler:function(comp){
                this.amtComp = comp
            },
            setProductNo:function(pd_no){
                this.pd_no = pd_no
            },

            save:function(){
                const _this = this;
                weui.form.validate('#addappoint',function(error) {
                    if(error) {
                        //$(error.ele).addClass('error')
                        $(error.ele).one('blur', function () {
                            $(this).removeClass('error')
                        })
                    }else{
                        var date=_this.$el.find('.appointdate').val(),
                        apptype=_this.$el.find('input[name="appoint_type"]:checked').val();
                        date=date.replace(/[^1234567890.]+/g, '');
                        var appt_amt,appo_share,strtype;
                        if( apptype==1){
                            appt_amt=_this.$el.find('input[name="appt_amt"]').val();
                            appo_share=0;
                            appt_amt=appt_amt.replace(/[^1234567890.]+/g, '');
                            strtype="申购预约";
                        }else{
                            appo_share=_this.$el.find('input[name="appo_share"]').val();
                            appt_amt=0;
                            appo_share=appo_share.replace(/[^1234567890.]+/g, '');
                            strtype="赎回预约";
                        }
                        var param={pd_no:_this.pd_no,
                                    appt_amt:appt_amt,
                                    appo_share:appo_share,
                                    appoint_date:date,
                                    appoint_type:apptype
                        };
                        ajax.post('pfL.1.440',param).success(function (resp) {
                            if(!resp.resultMsg){
                                $.toast(strtype+'成功',1000,function(){
                                    LDFW.navigate('main')
                                })
                            }

                        })

                    }
                })
                return false;
            }
        })
        return new View()
    })
